package com.yuedao.sender.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.IOUtils;

public class ByteUtil {

	public static byte[] decompress(byte[] data){
		byte[] unzip = null;
		try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(data)), out);
            unzip = out.toByteArray();

        } catch (Exception e) {
            e.printStackTrace();
        }
		return unzip;
    }
	
	
	 public static byte[] compress(byte[] data) {  
	        if (data == null || data.length == 0) {  
	            return null;  
	        }  
	        ByteArrayOutputStream out = new ByteArrayOutputStream();  
	        GZIPOutputStream gzip;  
	        try {  
	            gzip = new GZIPOutputStream(out);  
	            gzip.write(data);  
	            gzip.close();  
	        } catch (IOException e) {  
	        }  
	        return out.toByteArray();  
	    }
	 
	     public  static byte[] toByteA(Object obj){
	    	 String ob=null;
	    	try {
	    		ob=new String(obj.toString().getBytes("utf-8"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	String[] s1 = ob.split(",");
	    	byte[] bytes = new byte[s1.length];    
	    	if(s1 != null && s1.length >0){
	    		for(int i =0;i < s1.length ; i++){
	    			bytes[i] = (byte) Integer.parseInt(s1[i]);
	    		}
	    	}
	    	return bytes;
	    }
	
}
